﻿using SD.Infrastructure.Repository.EntityFrameworkCore;
using SD.Infrastructure.RepositoryBase;
using SlamDunk.MES.Domain.Entities.StockContext;
using SlamDunk.MES.Domain.IRepositories.Interfaces.StockContext;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace SlamDunk.MES.Repository.Implements.StockContext
{
    /// <summary>
    /// 物料批次仓储实现
    /// </summary>
    public class MaterialLotRepository : EFAggRootRepositoryProvider<MaterialLot>, IMaterialLotRepository
    {
        #region # 同步锁 —— static object _Sync
        /// <summary>
        /// 同步锁
        /// </summary>
        private static readonly object _Sync = new object();
        #endregion

        #region # 分页获取物料批次列表 —— ICollection<MaterialLot> FindByPage(string keywords...
        /// <summary>
        /// 分页获取物料批次列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="materialId">物料定义Id</param>
        /// <param name="supplierId">供应商Id</param>
        /// <param name="startProducedDate">生产日期开始</param>
        /// <param name="endProducedDate">生产日期结束</param>
        /// <param name="startExpiredDate">过期日期开始</param>
        /// <param name="endExpiredDate">过期日期结束</param>
        /// <param name="startReceptionDate">接收日期开始</param>
        /// <param name="endReceptionDate">接收日期结束</param>
        /// <param name="materialSourceType">物料来源类型</param>
        /// <param name="facilityIds">工厂模型Id集</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="rowCount">总记录数</param>
        /// <param name="pageCount">总页数</param>
        /// <returns>物料批次列表</returns>
        public ICollection<MaterialLot> FindByPage(string keywords, Guid? materialId, Guid? supplierId, DateTime? startProducedDate, DateTime? endProducedDate, DateTime? startExpiredDate, DateTime? endExpiredDate, DateTime? startReceptionDate, DateTime? endReceptionDate, MaterialSourceType? materialSourceType, IList<Guid> facilityIds, int pageIndex, int pageSize, out int rowCount, out int pageCount)
        {
            startProducedDate = startProducedDate?.Date;
            endProducedDate = endProducedDate?.Date;
            startExpiredDate = startExpiredDate?.Date;
            endExpiredDate = endExpiredDate?.Date;
            startReceptionDate = startReceptionDate?.Date;
            endReceptionDate = endReceptionDate?.Date;

            Expression<Func<MaterialLot, bool>> condition =
                x =>
                    (string.IsNullOrEmpty(keywords) || x.Keywords.Contains(keywords)) &&
                    (materialId == null || x.MaterialId == materialId) &&
                    (supplierId == null || x.SupplierId == supplierId) &&
                    (startProducedDate == null || x.ProducedDate >= startProducedDate) &&
                    (endProducedDate == null || x.ProducedDate <= endProducedDate) &&
                    (startExpiredDate == null || x.ExpiredDate >= startExpiredDate) &&
                    (endExpiredDate == null || x.ExpiredDate <= endExpiredDate) &&
                    (startReceptionDate == null || x.ReceptionDate >= startReceptionDate) &&
                    (endReceptionDate == null || x.ReceptionDate <= endReceptionDate) &&
                    (materialSourceType == null || x.MaterialSourceType == materialSourceType);

            IQueryable<MaterialLot> materialLots = base.Find(condition);
            if (facilityIds != null && facilityIds.Any())
            {
                materialLots = materialLots.Where(x => x.FacilityId.HasValue && facilityIds.Contains(x.FacilityId.Value));
            }

            IOrderedQueryable<MaterialLot> orderedResult = materialLots.OrderByDescending(x => x.AddedTime);
            IQueryable<MaterialLot> pagedResult = orderedResult.ToPage(pageIndex, pageSize, out rowCount, out pageCount);

            return pagedResult.ToList();

        }
        #endregion

        #region # 获取物料批次 —— MaterialLot SingleOrDefault(Guid materialId, Guid supplierId...
        /// <summary>
        /// 获取物料批次
        /// </summary>
        /// <param name="materialId">物料定义Id</param>
        /// <param name="supplierId">供应商Id</param>
        /// <param name="productionBatchNo">生产批号</param>
        /// <param name="producedDate">生产日期</param>
        /// <param name="guaranteePeriod">保质期</param>
        /// <param name="expiredDate">过期日期</param>
        /// <param name="receptionDate">接收日期</param>
        /// <param name="specification">规格</param>
        /// <returns>物料批次</returns>
        public MaterialLot SingleOrDefault(Guid materialId, Guid supplierId, string productionBatchNo, DateTime producedDate, TimeSpan guaranteePeriod, DateTime expiredDate, DateTime receptionDate, string specification)
        {
            long guaranteePeriodTicks = guaranteePeriod.Ticks;

            Expression<Func<MaterialLot, bool>> condition =
                x =>
                    (x.MaterialId == materialId) &&
                    (x.SupplierId == supplierId) &&
                    (x.ProductionBatchNo == productionBatchNo) &&
                    (x.ProducedDate == producedDate) &&
                    (x.GuaranteePeriodTicks == guaranteePeriodTicks) &&
                    (x.ExpiredDate == expiredDate) &&
                    (x.ReceptionDate == receptionDate) &&
                    (x.Specification == specification);

            lock (_Sync)
            {
                MaterialLot materialLot = base.SingleOrDefault(condition);

                return materialLot;
            }
        }
        #endregion

        #region # 是否存在物料批次 —— bool Exists(Guid materialId, Guid supplierId...
        /// <summary>
        /// 是否存在物料批次
        /// </summary>
        /// <param name="materialId">物料定义Id</param>
        /// <param name="supplierId">供应商Id</param>
        /// <param name="productionBatchNo">生产批号</param>
        /// <param name="producedDate">生产日期</param>
        /// <param name="guaranteePeriod">保质期</param>
        /// <param name="expiredDate">过期日期</param>
        /// <param name="receptionDate">接收日期</param>
        /// <param name="specification">规格</param>
        /// <returns>是否存在</returns>
        public bool Exists(Guid materialId, Guid supplierId, string productionBatchNo, DateTime producedDate, TimeSpan guaranteePeriod, DateTime expiredDate, DateTime receptionDate, string specification)
        {
            long guaranteePeriodTicks = guaranteePeriod.Ticks;

            Expression<Func<MaterialLot, bool>> condition =
                x =>
                    (x.MaterialId == materialId) &&
                    (x.SupplierId == supplierId) &&
                    (x.ProductionBatchNo == productionBatchNo) &&
                    (x.ProducedDate == producedDate) &&
                    (x.GuaranteePeriodTicks == guaranteePeriodTicks) &&
                    (x.ExpiredDate == expiredDate) &&
                    (x.ReceptionDate == receptionDate) &&
                    (x.Specification == specification);

            return base.Exists(condition);
        }
        #endregion
    }
}
